@model IEnumerable<VotingSystem.ViewModels.OrderViewModel>
@using VotingSystem.Models

@{
    ViewData["Title"] = "订单列表";
}

<div class="container">
    <div class="row mb-4">
        <div class="col-md-8">
            <h1 class="display-4">订单列表</h1>
        </div>
        <div class="col-md-4 text-end">
            <a asp-action="Create" class="btn btn-primary btn-lg">
                <i class="fas fa-plus"></i> 创建新订单
            </a>
        </div>
    </div>

    <div class="row mb-4">
        <div class="col-md-6">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="搜索订单..." id="searchInput">
                <button class="btn btn-outline-secondary" type="button" id="searchButton">
                    <i class="fas fa-search"></i>
                </button>
            </div>
        </div>
        <div class="col-md-6">
            <div class="btn-group float-end">
                <button type="button" class="btn btn-outline-primary active" data-filter="all">全部</button>
                <button type="button" class="btn btn-outline-primary" data-filter="pending">待处理</button>
                <button type="button" class="btn btn-outline-primary" data-filter="processing">处理中</button>
                <button type="button" class="btn btn-outline-primary" data-filter="completed">已完成</button>
            </div>
        </div>
    </div>

    <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
        @foreach (var order in Model)
        {
            <div class="col order-item" 
                 data-status="@order.Status.ToString().ToLower()"
                 data-creator="@(User.Identity?.Name == order.CreatorName ? "my" : "other")">
                <div class="card h-100 shadow-sm">
                    <div class="card-body">
                        <h5 class="card-title">@order.Title</h5>
                        <p class="card-text text-muted small">
                            <i class="fas fa-user"></i> @order.CreatorName
                            <br />
                            <i class="fas fa-clock"></i> @order.CreateTime.ToString("yyyy-MM-dd HH:mm")
                            <br />
                            <i class="fas fa-hourglass-end"></i> 截止时间：@order.Deadline.ToString("yyyy-MM-dd HH:mm")
                        </p>
                        <p class="card-text">@(order.Description?.Length > 100 ? order.Description.Substring(0, 100) + "..." : order.Description)</p>
                        
                        <div class="progress mb-3" style="height: 20px;">
                            <div class="progress-bar bg-success" role="progressbar" 
                                 style="width: @(order.Progress)%">
                                @order.Progress%
                            </div>
                        </div>

                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <span class="badge @GetStatusBadgeClass(order.Status)">
                                @GetStatusText(order.Status)
                            </span>
                            <span class="badge @(order.Priority == OrderPriority.High ? "bg-danger" : order.Priority == OrderPriority.Medium ? "bg-warning" : "bg-info")">
                                @GetPriorityText(order.Priority)
                            </span>
                        </div>

                        <div class="d-grid gap-2">
                            <a asp-action="Details" asp-route-id="@order.Id" class="btn btn-outline-primary">
                                <i class="fas fa-info-circle"></i> 查看
                            </a>
                            @if (order.CanEdit)
                            {
                                <a asp-action="Edit" asp-route-id="@order.Id" class="btn btn-outline-warning">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                            }
                            @if (order.CanDelete)
                            {
                                <form asp-action="Delete" method="post" class="d-grid" onsubmit="return confirm('确定要删除此订单吗？此操作不可恢复！');">
                                    <input type="hidden" name="id" value="@order.Id" />
                                    <button type="submit" class="btn btn-outline-danger">
                                        <i class="fas fa-trash"></i> 删除
                                    </button>
                                </form>
                            }
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>

    @if (!Model.Any())
    {
        <div class="text-center text-muted my-5">
            <i class="fas fa-inbox fa-3x mb-3"></i>
            <h4>暂无订单</h4>
            @if (User.Identity?.IsAuthenticated == true)
            {
                <p>快来创建第一个订单吧！</p>
                <a asp-action="Create" class="btn btn-primary">
                    <i class="fas fa-plus"></i> 创建订单
                </a>
            }
            else
            {
                <p>请登录后创建订单</p>
                <a asp-controller="Account" asp-action="Login" class="btn btn-primary">
                    <i class="fas fa-sign-in-alt"></i> 登录
                </a>
            }
        </div>
    }
</div>

@section Scripts {
    <script>
        // 搜索和筛选功能
        document.addEventListener('DOMContentLoaded', function() {
            const searchInput = document.getElementById('searchInput');
            const searchButton = document.getElementById('searchButton');
            const filterButtons = document.querySelectorAll('[data-filter]');
            const orderItems = document.querySelectorAll('.order-item');

            function filterOrders() {
                const searchText = searchInput.value.toLowerCase();
                const activeFilter = document.querySelector('[data-filter].active').dataset.filter;

                orderItems.forEach(item => {
                    const title = item.querySelector('.card-title').textContent.toLowerCase();
                    const description = item.querySelector('.card-text').textContent.toLowerCase();
                    const status = item.dataset.status;
                    const creator = item.dataset.creator;

                    const matchesSearch = title.includes(searchText) || description.includes(searchText);
                    const matchesFilter = activeFilter === 'all' || 
                                       (activeFilter === 'pending' && status === 'pending') ||
                                       (activeFilter === 'processing' && status === 'processing') ||
                                       (activeFilter === 'completed' && status === 'completed');

                    item.style.display = matchesSearch && matchesFilter ? '' : 'none';
                });
            }

            searchButton.addEventListener('click', filterOrders);
            searchInput.addEventListener('input', filterOrders);

            filterButtons.forEach(button => {
                button.addEventListener('click', function() {
                    filterButtons.forEach(btn => btn.classList.remove('active'));
                    this.classList.add('active');
                    filterOrders();
                });
            });
        });
    </script>
}

@section Styles {
    <style>
        .hover-shadow:hover {
            transform: translateY(-5px);
            box-shadow: 0 .5rem 1rem rgba(0,0,0,.15)!important;
            transition: all .3s ease;
        }
        .card {
            transition: all .3s ease;
        }
        .btn-group .btn {
            padding: .375rem .75rem;
        }
    </style>
}

@functions {
    string GetStatusBadgeClass(OrderStatus status)
    {
        return status switch
        {
            OrderStatus.Pending => "bg-warning",
            OrderStatus.Processing => "bg-info",
            OrderStatus.Completed => "bg-success",
            OrderStatus.Cancelled => "bg-danger",
            _ => "bg-secondary"
        };
    }

    string GetStatusText(OrderStatus status)
    {
        return status switch
        {
            OrderStatus.Pending => "待处理",
            OrderStatus.Processing => "处理中",
            OrderStatus.Completed => "已完成",
            OrderStatus.Cancelled => "已取消",
            _ => "未知状态"
        };
    }

    string GetPriorityText(OrderPriority priority)
    {
        return priority switch
        {
            OrderPriority.High => "高优先级",
            OrderPriority.Medium => "中优先级",
            OrderPriority.Low => "低优先级",
            _ => "未知优先级"
        };
    }
} 